﻿package yii.base{

	import yii.*;
	import phpas3.*;
	import yii.db.*;

	public class CApplication extends CModule{

	/**
	 * Constructor+
	 * @param mixed $config application configuration+
	 * If a string, it is treated as the path of the file that contains the configuration;
	 * If an array, it is the actual configuration information+
	 * Please make sure you specify the {@link getBasePath basePath} property in the configuration,
	 * which should point to the directory containing all application logic, template and data+
	 * If not, the directory will be defaulted to 'protected'+
	 */
	public function CApplication($config:Object=null){
		Yii.setApplication(this);

		// set basePath at early as possible to avoid trouble
//		if( isset($config['basePath']) ){
//			this.setBasePath( $config['basePath'] );
//			delete $config['basePath'] ;
//		} else {
//			this.setBasePath('protected');
//		}
//		Yii.setPathOfAlias('application',this.getBasePath());
//		Yii.setPathOfAlias('webroot',dirname($_SERVER['SCRIPT_FILENAME']));
//		Yii.setPathOfAlias('ext',this.getBasePath()+DIRECTORY_SEPARATOR+'extensions');

		this.preinit();

		this.initSystemHandlers();
		this.registerCoreComponents();

		this.configure($config);
		//this.attachBehaviors(this.behaviors);
		this.preloadComponents();

		this.init();
	}



	/**
	 * Registers the core application components+
	 * @see setComponents
	 */
	protected function registerCoreComponents():void{
		var $components:Object = {
			"db":{
				"class":'yii.db.CDbConnection'
			}
		}
		this.setComponents( $components );
	}






	/**
	 * Initializes the class autoloader and error handlers+
	 */
	protected function initSystemHandlers():void{
//		if(YII_ENABLE_EXCEPTION_HANDLER)
//			set_exception_handler(array(this,'handleException'));
//		if(YII_ENABLE_ERROR_HANDLER)
//			set_error_handler(array(this,'handleError'),error_reporting());
	}

















	/**
	 * Runs the application+
	 * This method loads static application components+ Derived classes usually overrides this
	 * method to do more application-specific tasks+
	 * Remember to call the parent implementation so that static application components are loaded+
	 */
	public function run(){
//		if(this.hasEventHandler('onBeginRequest'))
//			this.onBeginRequest(new CEvent(this));
//		this.processRequest();
//		if(this.hasEventHandler('onEndRequest'))
//			this.onEndRequest(new CEvent(this));
	}













	/**
	 * Returns the database connection component+
	 * @return CDbConnection the database connection
	 */
	public function getDb():CDbConnection{
		return this.getComponent('db');
	}




	}
}